package com.yh.doing.observer_pattern.version3;

import com.yh.doing.observer_pattern.version1.IHanFeiZi;

import java.util.ArrayList;

/**
 * Created by Rocky on xxxx-xx-xx.
 */
public class HanFeiZi implements Observable, IHanFeiZi {
    private ArrayList<Observer> observerList = new ArrayList<Observer>();

    public void addObserver(Observer observer) {
        this.observerList.add(observer);
    }

    //删除观察者
    public void deleteObserver(Observer observer) {
        this.observerList.remove(observer);
    }

    //通知所有的观察者
    public void notifyObservers(String context) {
        for (Observer observer : observerList) {
            observer.update(context);
        }
    }


    @Override
    public void haveBreakfast() {
        System.out.println("韩非子吃饭");
        this.notifyObservers("韩非子在吃饭");
    }

    @Override
    public void haveFun() {
        System.out.println("韩非子娱乐");
        this.notifyObservers("韩非子娱乐");
    }


}
